package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.DestinationQueryObject;
import cn.wolfcode.wolf2w.repository.DestinationRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.util.DBHelp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
//@Transactional
public class DestinationServiceImpl implements IDestinationService {


    @Autowired
    private DestinationRepository repository;
    @Autowired
    private IRegionService regionService;
    @Autowired
    private MongoTemplate template;

    @Override
    public void save(Destination destination) {
        repository.save(destination);

    }

    @Override
    public void update(Destination destination) {
        repository.save(destination);

    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);

    }

    @Override
    public Destination get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public List<Destination> list() {
        return repository.findAll();
    }

    @Override
    public List<Destination> getDestByRegionId(String rid) {
        //获取区域对象
        Region region = regionService.get(rid);
        List<String> refIds = region.getRefIds();
        //遍历id集合获取目的地,使用JPA的In方法
        return repository.findByIdIn(refIds);
    }

    //分页查询
    @Override
    public Page<Destination> query(DestinationQueryObject qo) {
        Query query = new Query();

        if (StringUtils.hasLength(qo.getKeyword())){
            query.addCriteria(Criteria.where("name").is(qo.getKeyword()));
        }

        if(StringUtils.hasLength(qo.getParentId())){
            query.addCriteria(Criteria.where("parentId").is(qo.getParentId()));
        }else{
            //为null
          query.addCriteria(Criteria.where("parentId").is(null));
        }


        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(), Sort.Direction.ASC,"_id");
        return DBHelp.query(template, Destination.class, pageable, query);
    }

    //导航吐司
    @Override
    public List<Destination> queryToastByParentId(String parentId) {
        List<Destination> list = new ArrayList<>();

        //递归
        this.createToast(list, parentId);

        //集合顺序翻转
        Collections.reverse(list);

        return list;
    }

    private void createToast(List<Destination> list, String parentId) {
        //parentID为null时候, 不执行
        if(!StringUtils.hasLength(parentId)){
            return;
        }
        Destination dest = this.get(parentId);
        list.add(dest);
        if(StringUtils.hasLength(dest.getParentId())){
            createToast(list, dest.getParentId());
        }
    }

    //更新添加
    @Override
    public void saveOrUpdate(Destination destination) {
        if(StringUtils.hasLength(destination.getId())){
            this.update(destination);
        }else{
            this.save(destination);
        }
    }

    //目的地切换
    @Override
    public List<Destination> getDestByRegionIdForApi(String regionId) {

        List<Destination> list;
        if ("-1".equals(regionId)){
            //特殊情况,需要根据name去查询中国所有省份
            list = repository.findByParentName("中国");
        }else {
            list = this.getDestByRegionId(regionId);
        }
        //遍历子目的地
        for (Destination destination : list){
            //通过ParentId去查询子目的地
            Pageable pageable = PageRequest.of(0, 5, Sort.Direction.ASC,"_id");
            List<Destination> child = repository.findByParentId(destination.getId(),pageable);
            destination.setChildren(child);
        }

        return list;
    }

    @Override
    public Destination queryByName(String name) {
        return repository.findByName(name);
    }




}
